package cn.edu.sdjzu.xg.bysj.controller.basic;

import cn.edu.sdjzu.xg.bysj.domain.GraduateProject;
import cn.edu.sdjzu.xg.bysj.service.GraduateProjectCacheService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import util.Condition;
import util.JSONUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;

@Slf4j
@WebServlet("/cache.ctl")
public class GraduateProjectCacheController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
            throws ServletException, IOException {
        //读取参数id
        String teacherId = request.getParameter("teacherId");
        Integer teacherId_int =Integer.parseInt(teacherId);
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        try {
            Collection<GraduateProject> graduateProjects =
                    GraduateProjectCacheService.getInstance().findByTeacherId(teacherId_int);
            message.put("我的课题",graduateProjects);

        } catch (SQLException e) {
            message.put("message", "数据库操作异常");
            log.error(e.getMessage());
        } catch (Exception e) {
            message.put("message", "网络异常");

            //打印异常栈，方便调试
            log.error(e.getMessage());
        }finally {
            //响应到前端
            response.getWriter().println(message);
        }
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String graduateProject_json = JSONUtil.getJSON(request);
        //将JSON字串解析为对象
        GraduateProject graduateProjectToAdd =
                JSON.parseObject(graduateProject_json,GraduateProject.class);
        graduateProjectToAdd.getGraduateProjectStatus().setId(2);
        graduateProjectToAdd.getGraduateProjectStatus().setDescription("暂存");
        System.out.println(graduateProjectToAdd);
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //在数据库表中增加对象
        try {
            if(GraduateProjectCacheService.getInstance().add(graduateProjectToAdd)){
                message.put("message", "增加成功");
                log.debug("增加成功");
            }else {
                message.put("message", "增加失败");
            }
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
            log.error(e.getMessage());
        }catch(Exception e){
            message.put("message", "网络异常");
            log.error(e.getMessage());
        }finally {
            //响应message到前端
            response.getWriter().println(message);
        }
    }


    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String request_json= JSONUtil.getJSON(request);
        //将JSON字串解析为对象
        GraduateProject graduateProjectToUpdate = JSON.parseObject(request_json, GraduateProject.class);
        System.out.println(graduateProjectToUpdate);
        //创建JSON对象message，以便往前端响应信息
        JSONObject message = new JSONObject();
        //到数据库表修改对象对应的记录
        try {
            if( GraduateProjectCacheService.getInstance().update(graduateProjectToUpdate)) {
                message.put("message", "修改成功");
                log.debug("修改成功");
            }else {
                message.put("message", "修改失败");
            }
        }catch (SQLException e){
            message.put("message", "数据库操作异常");
            e.printStackTrace();
        }catch(Exception e){
            message.put("message", "网络异常");
            log.error(e.getMessage());
        }
        //响应message到前端
        response.getWriter().println(message);
    }
    @Override
    protected void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //读取参数id
        String id_str = request.getParameter("id");
        //读取请求body
        String request_json= JSONUtil.getJSON(request);
        JSONObject req_jsonObj = JSON.parseObject(request_json);
        JSONArray List_jsonArray =
                req_jsonObj.getJSONArray("ListToDelete");
        List<GraduateProject> listToDelete = null;
        //创建JSON对象respMessage_jsonObj，以便往前端响应信息
        JSONObject respMessage_jsonObj = new JSONObject();
        try {
            if(id_str!=null) {
                int id = Integer.parseInt(id_str);
                //到数据库表中删除对应的记录
                GraduateProjectCacheService.getInstance().delete(id);
                respMessage_jsonObj.put("message", "删除成功");
                log.debug("删除成功");
            }
            else if(List_jsonArray!=null){
                listToDelete= List_jsonArray.toJavaList(GraduateProject.class);
                GraduateProjectCacheService.getInstance().deletes(listToDelete);
                respMessage_jsonObj.put("message", "批量删除成功");
                log.debug("批量删除成功");
            }else {
                respMessage_jsonObj.put("message", "删除失败");
            }
        } catch (SQLException e) {
            respMessage_jsonObj.put("message", "数据库操作异常");
            //打印异常栈，方便调试
            log.error(e.getMessage());
        } catch (Exception e) {
            respMessage_jsonObj.put("message", "网络异常");
            //打印异常栈，方便调试
            log.error(e.getMessage());
        }
        //响应respMessage_jsonObj到前端
        response.getWriter().println(respMessage_jsonObj);
    }
}